<template>
  <div class="header">
    <div>
      <h3 class="header_title">叩丁狼电商运营平台</h3>
      <el-icon color="#ffffff" :size="20" @click="isCollapse = !isCollapse"
        ><Menu
      /></el-icon>
    </div>
    <div>
      <el-dropdown @command="commandHandle">
        <span class="el-dropdown-link">
          {{ userStore.username }}
          <el-icon class="el-icon--right">
            <arrow-down />
          </el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="index">首页</el-dropdown-item>
            <el-dropdown-item command="logout">退出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Menu } from "@element-plus/icons-vue";
import { ArrowDown } from "@element-plus/icons-vue";
import { inject } from "vue";
import { useUserStore } from "@/store/user";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { logout } from "@/common";
import { loingOutApi } from "@/http/api/index";
const isCollapse = inject("isCollapse");
const userStore = useUserStore();
const router = useRouter();
const commandHandle = (command: string) => {
  if (command === "index") {
    // 跳转到首页
    router.push("/index");
  } else if (command === "logout") {
    // 退出登录
    logout(
      () => {
        loingOutApi();
      },
      () => {
        // 提示
        ElMessage.success("退出成功");
      }
    );
  }
};
</script>

<style scoped lang="less">
.header_title {
  line-height: 70px;
  font-weight: 400;
  font-size: 24px;
  color: #fff;
  display: inline-block;
  padding-right: 20px;
}
.example-showcase .el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  :deep(.el-dropdown) {
    color: #fff;
  }
}
</style>
